package org.an.proj.contacts.config.manager;

import android.support.annotation.DrawableRes;
import android.support.v4.app.Fragment;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Map;
import org.an.proj.contacts.R;
import org.an.proj.contacts.model.vo.URL;
import org.an.proj.contacts.ui.contact.ContactFragment;
import org.an.proj.contacts.ui.mine.MineFragment;
import org.an.proj.contacts.ui.msg.MsgFragment;

/**
 * 添加对Url解析工具类 UrlSchemeUtils
 */
public class ConfigManager {

  //H5分享使用？
  public static final String SCHEME = "contact";

  public static String buildActionUrl(String baseUrl, Map<String, String> params) {
    if (params == null) {
      return baseUrl;
    }

    String query = "";
    StringBuilder sb = new StringBuilder();
    for (String key : params.keySet()) {
      sb.append(key + "=" + params.get(key) + "&");
    }

    if (sb.length() > 1) {
      query = sb.substring(0, sb.length() - 1);
      try {
        return baseUrl + "?" + URLEncoder.encode(query, "utf-8");
      } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
      }
    }

    return baseUrl;
  }

  public static String buildActionUrl(String baseUrl, Object obj) {
    if (obj == null) {
      return baseUrl;
    }

    String query = "";
    StringBuilder sb = new StringBuilder();
    Class<?> cls = obj.getClass();
    Field[] fields = cls.getFields();
    for (int i = 0; i < fields.length; i++) {
      try {
        Field field = fields[i];
        Object fieldValue = field.get(obj);
        if (fieldValue != null) {
          sb.append(field.getName() + "=" + fieldValue.toString() + "&");
        }
      } catch (Exception e) {
        e.printStackTrace();
        continue;
      }
    }

    if (sb.length() > 1) {
      query = sb.substring(0, sb.length() - 1);
      try {
        return baseUrl + "?" + URLEncoder.encode(query, "utf-8");
      } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
      }
    }

    return baseUrl;
  }

  public static String decodeUrl(String urlString) {
    try {
      return URLDecoder.decode(urlString, "utf-8");
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }
    return null;
  }

  public static @DrawableRes int getTabImg(String actionUrl) {
    URL url = URL.parse(actionUrl);
    int res = 0;
    if (url != null) {
      if (SCHEME.equals(url.getScheme())) {
        if (url.getAction() != null) {
          switch (url.getAction()) {
            case ACTION_URL_TAB.msg:
              res = R.drawable.icon_msg_selector;
              break;
            case ACTION_URL_TAB.contact:
              res = R.drawable.icon_contact_selector;
              break;
            case ACTION_URL_TAB.mine:
              res = R.drawable.icon_mine_selector;
              break;
          }
        }
      }
    }
    return res != 0 ? res : R.drawable.ic_launcher;
  }

  /**
   * http://baike.baidu.com/link?url=
   * lianjia://web/h5?title=楼盘动态&url=xxxx
   */
  public static Fragment getTabFragment(String actionUrl) {
    URL url = URL.parse(actionUrl);
    Fragment fragment = null;

    if (url != null) {
      if (SCHEME.equals(url.getScheme())) {
        if (url.getAction() != null) {
          switch (url.getAction()) {
            case ACTION_URL_TAB.msg:
              fragment = new MsgFragment();
              break;
            case ACTION_URL_TAB.contact:
              fragment = new ContactFragment();
              break;
            case ACTION_URL_TAB.mine:
              fragment = new MineFragment();
              break;
          }
        }
      }
    }

    return fragment == null ? new Fragment() : fragment;
  }

  public interface ACTION_URL_TAB {
    String msg = "tab/message";
    String contact = "tab/contacts";
    String mine = "tab/mine";
  }

  public interface ACTION_URL_MENU {

    String mymenu_action_rating = "to be continue";

    String mymenu_action_workload = "web/compaign?navtitle=我的行程量";

    String mymenu_action_recommend = "app/recommend";

    String mymenu_action_setting = "setting/detail";

    String mymenu_action_about = "about/linknh";

    String mymenu_action_about_channel = "about/channel";
  }

  /**
   * 解码url
   */
  public static String getRealString(String url) {
    if (url == null) {
      return null;
    }

    String strUrl;
    try {
      strUrl = URLDecoder.decode(url, "UTF-8");
      return strUrl;
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }

    return null;
  }
}
